﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace Common.Util
{
    public class SHAEncryptor
    {
        /// <summary>
        /// 通过SHA256加密明文
        /// <summary>
        /// <param name="strSource">需要加密的明文</param>
        /// <returns>返回加密结果</returns>
        public static string SHA256Encrypt(string strSource)
        {
            SHA256 sha = SHA256.Create();
            //获取密文字节数组
            byte[] bytResult = sha.ComputeHash(System.Text.Encoding.Default.GetBytes(strSource));
            //转换成字符串，并取9到25位
            //string strResult = BitConverter.ToString(bytResult, 4, 8);
            //转换成字符串，32位
            string strResult = BitConverter.ToString(bytResult);
            //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符，需要去除掉
            strResult = strResult.Replace("-", "");

            return strResult;
        }

        /// <summary>
        /// 通过SHA512加密明文
        /// <summary>
        /// <param name="strSource">需要加密的明文</param>
        /// <returns>返回加密结果</returns>
        public static string SHA512Encrypt(string strSource)
        {
            //string strIN = getstrIN(strIN);
            byte[] tmpByte;
            SHA512 sha512 = new SHA512Managed();
            ASCIIEncoding ascii = new ASCIIEncoding();

            tmpByte = sha512.ComputeHash(ascii.GetBytes(strSource));

            sha512.Clear();

            return ascii.GetString(tmpByte);
        }

      

    }
}
